package ca.uwindsor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TimePicker;

public class AddSchedule extends Activity {

	String course_code;
	String day;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_schedule);
		
		//Populate course code spinner
		  // DID :) change this value to the global term value
		fillSpinner(Common.globalTerm);
		
		
		
		Button add_but = (Button) findViewById(R.id.add_button);
		
		Spinner sCourseCode = (Spinner) findViewById(R.id.class_coursecode);
		Spinner sDay = (Spinner) findViewById(R.id.spinner_day);
		sCourseCode.setOnItemSelectedListener(new MyOnItemSelectedListener());
		sDay.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
		// Alert Dialog builder
				final AlertDialog.Builder alert = new AlertDialog.Builder(this);
				
				add_but.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						// TODO Auto-generated method stub
						final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
						db.setDATABASE_TABLE("class_schedule");
						
						db.open();
						
						//TODO: get values from form
						
						TimePicker startTime = (TimePicker)findViewById(R.id.startTime);
						TimePicker endTime = (TimePicker)findViewById(R.id.endTime);
						
						String txtStartTime = startTime.getCurrentHour().toString()
												+":"+startTime.getCurrentMinute().toString();
						
						String txtEndTime = endTime.getCurrentHour().toString()
											+":"+endTime.getCurrentMinute().toString();
						
						if(new Common().CompareTime(txtEndTime, txtStartTime)<=0)
						{
							alert.setMessage("End time should be greater than start time");
							alert.setNegativeButton("OK", new OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
								}

							});
							alert.show();
						}
						else{			
						
						String where = "sem_name" + "= '" + Common.globalTerm+"' and "
										+ "course_code" + "= '" + course_code+"' and "
										+ "Day" + "= '" + day+"' and "
										+"StartTime" + "= '" +txtStartTime +"' and "
										+"EndTime" + "= '" +txtEndTime +"'";
						Cursor ret = db.getEntry(where);
						
						if(ret.getCount()==0){
							//add course
							ContentValues newValues = new ContentValues();
							newValues.put("sem_name", Common.globalTerm);
							
							newValues.put("course_code", course_code);
							
							newValues.put("Day", day);
							
							newValues.put("StartTime", txtStartTime);
							
							newValues.put("EndTime", txtEndTime);
							
							db.insertEntry(newValues);
							
							alert.setMessage("Schedule added for course "+course_code);

							alert.setNegativeButton("OK", new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									startActivity(new Intent("ca.uwindsor.SUMMARY"));
								}

							});
							alert.setCancelable(false);
							alert.show();
							
						}else{ //show dialog
							// Alert the user
							alert.setMessage("Schedule already exists");

							alert.setNegativeButton("Go Back", new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									// do nothing
								}

							});
							alert.show();
							
							
						}
						db.close();
					}
					}
				});
	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
	
	
	public void fillSpinner(String term)
	{
		MyDBAdapter db = new MyDBAdapter(this);
		db.setDATABASE_TABLE("courses");
		db.open();
		// create an array adapter
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// gets all the entries
		Cursor c = db.getEntry("sem_name", term);
		
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		
		//if no courses exist forward to add courses
		if(c.getCount()==0){
			alert.setMessage("No courses added yet. Please add courses first.");

			alert.setNegativeButton("OK", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					startActivity(new Intent("ca.uwindsor.ADDCOURSES"));
				}

			});
			
			db.close();
			alert.setCancelable(false);
			alert.show();
		}
		
		if (c.moveToFirst()) {
			do {
				adapter.add(c.getString(2));
			} while (c.moveToNext());
		}
		Spinner s = (Spinner) findViewById(R.id.class_coursecode);
		s.setAdapter(adapter);
		db.close();
	}
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			// System.out.print(parent.getAdapter().toString());
			switch (parent.getId()) {
			case R.id.class_coursecode:
				course_code = parent.getItemAtPosition(pos).toString();
				break;
			case R.id.spinner_day:
				day = parent.getItemAtPosition(pos).toString();
				break;

			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	
	
}
